<?php
namespace system\libraries;

use system\core\Config;
use system\core\Request;

/**
 * Translation
 * 
 * @author Steffen Brem <steffenbrem@gmail.com>
 * @package system.libraries
 */
class Translation 
{
    /**
     * Default Language
     * 
     * @var string
     */
    private static $_language;
    
    /**
     * Stores all the language lines
     * 
     * @var array
     */
    private static $_lines = array();
    
    /**
     * Constructor
     * 
     * @access private
     */
    public function __construct()
    {
        if ( ! self::$_language )
        {
            self::$_language = Config::get('translation', 'language');
        }
        
        // Check if the language directory exists
        if ( ! file_exists(APP_PATH . 'language/' . self::$_language) )
        {
            throw new \Exception('The language "' . Config::get('translation', 'language') . '" does not exist.');
        }
        
        // Let's load the translation helper, so we have easy access for fetching
        // lines.
        require_once SYS_PATH . 'helpers/translation_helper.php';
    }
    
    /**
     * Fetch a line from the lines array
     * 
     * @param string $line
     * @return string
     */
    public function line( $line )
    {
        if ( isset(self::$_lines[$line]) )
        {
            return self::$_lines[$line];
        }
        
        throw new \Exception('Could not fetch language line "' . $line . '".');
    }
    
    /**
     * Set language
     * 
     * @param string $language
     */
    public static function setLanguage( $language )
    {
        self::$_language = $language;
    }
    
    /**
     * Load a language file
     * 
     * @param string $filename
     * @param string $language
     */
    public function load( $filename, $language = NULL )
    {
        // If no language was passed, we will use the default language
        if ( ! $language )
        {
            $language = self::$_language;
        }
        
        $request = Request::getInstance();
        
        $appLangPath = ( $request->getModule() ) 
                    ? APP_PATH . 'modules/' . $request->getModule() . '/language/' . $language . '/' . $filename . '_lang.php'
                    : APP_PATH . 'language/' . $language . '/' . $filename . '_lang.php';
        
        $sysLangPath = SYS_PATH . 'language/' . $language . '/' . $filename . '_lang.php';
        
        // First check if the language file is in the system folder, load it.
        if ( file_exists($sysLangPath) )
        {
            include $sysLangPath;
            
            $this->_setMemory($lang, $language, $filename);
        }
        
        // Check if the language file is in the application folder, if so, load
        // and if neccessary, overwrite!
        if ( file_exists($appLangPath) )
        {
            include $appLangPath;
            
            $this->_setMemory($lang, $language, $filename);
        }
    }
    
    /**
     * Set the language array in the Translation memory
     * 
     * @param array $lang
     */
    private function _setMemory( $lang = NULL , $language, $filename )
    {
        if ( ! isset($lang) || ! is_array($lang) )
        {
            throw new \Exception('The language file "' . $language . '/' . $filename . '_lang.php" is corrupt.
                Make sure you store them as $lang.');
        }

        foreach ( $lang as $line => $value )
        {
            self::$_lines[$line] = $value;
        }
    }
}

/* End of file Translation.php */